package edu.wtbu.cs.androiddemo.activity.list;

import android.app.AlertDialog;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import edu.wtbu.cs.androiddemo.R;

public class ListViewSampleActivity extends AppCompatActivity {

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
//        setContentView(R.layout.activity_list_view_sample);
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
//            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
//            return insets;
//        });
//    }
    private ListView mListView;
    private MyBaseAdapter myBaseAdapter;
    private int[] icons = {R.drawable.a, R.drawable.b, R
            .drawable.c, R.drawable.d, R.drawable.e,
            R.drawable.f};
    private String[] titles = {"白鹃梅", "五色椒", "小檗",
            "李叶绣线菊", "多花兰", "獐耳细辛"};
    private String[] depicts =
            {"白鹃梅又名茧子花、金瓜果等，是蔷薇科白鹃梅属灌木。",
                    "五色椒又名朝天椒，五彩辣椒，为辣椒 变种，味涩",
                    "小檗春日黄花簇簇，秋日红果满枝", "蔷薇科、绣线菊属灌木，高可达3米。",
                    "多花兰是兰科兰属的一种附生植物",
                    "植物獐耳细辛，分布于我国辽宁、安徽、浙江、河南"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_list_view_sample);
        mListView = (ListView) findViewById(R.id.listView);
        myBaseAdapter = new MyBaseAdapter();
        mListView.setAdapter(myBaseAdapter);

        mListView.setOnItemClickListener(new AdapterView
                .OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?>
                                            parent, View
                                            view, int
                                            position,
                                    long id) {
                String content = "植物名称：" +
                        titles[position] +
                        "。描述：" + depicts[position];
                Toast.makeText(ListViewSampleActivity.this,
                        content, Toast
                                .LENGTH_LONG).show();
            }
        });

        mListView.setOnItemLongClickListener(new AdapterView
                .OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?>
                                                   parent, View view, int position,
                                           long id) {
                AlertDialog alertDialog = new AlertDialog
                        .Builder
                        (ListViewSampleActivity.this).setMessage
                                ("确定要删除吗？")
                        .setPositiveButton("确定", null)
                        .setNegativeButton("取消", null)
                        .create();
                alertDialog.show();
                return true;
            }
        });
    }

    private class MyBaseAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int position) {
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View
                convertView, ViewGroup parent) {

            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(ListViewSampleActivity
                        .this, R
                        .layout.list_item, null);
                viewHolder = new ViewHolder();
                viewHolder.tv_title = (TextView) convertView
                        .findViewById(R.id.tv_title);
                viewHolder.tv_depict = (TextView)
                        convertView
                                .findViewById(R.id
                                        .tv_depict);
                viewHolder.image = (ImageView) convertView
                        .findViewById(R.id.iv);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView
                        .getTag();

            }
            viewHolder.image.setBackgroundResource
                    (icons[position]);
            viewHolder.tv_title.setText(titles[position]);
            viewHolder.tv_depict.setText(depicts[position]);
            return convertView;
        }
    }

    public class ViewHolder {
        TextView tv_title;
        TextView tv_depict;
        ImageView image;
    }
}